﻿	<cffunction name="cssCompress" returntype="string">
		<cfargument name="csscode" type="string" required="yes">
		<cfargument name="skip" default="false" required="false" type="boolean">

		<cfset var result = trim(arguments.csscode)>

		<cfif arguments.skip>
			<cfreturn arguments.jscode>
		</cfif>

		<cfset result = rereplacenocase(result, "/\*", "#chr(172)#", "all")>
		<cfset result = rereplacenocase(result, "\*/", "#chr(172)#", "all")>
		<cfset result = rereplacenocase(result, "#chr(172)#[^#chr(172)#]*#chr(172)#", "", "all")>

		<cfset result = rereplace(result, "[\s]{2,}", " ", "all")>
		<cfset result = rereplacenocase(result, "[\s]*([\{|\}|\(|\)|\;|\:]+)[\s]*", "\1", "all")>
		<cfset result = replaceNoCase(result, "url(", "url(css/", "all")>

		<cfreturn result>
	</cffunction>
<!---  --->
	<cffunction name="HTMLcompress" returntype="String">
		<cfargument name="htmlcode" required="true" type="string">
		<cfargument name="skip" default="false" required="false" type="boolean">

		<cfset var result = trim(arguments.htmlcode)>

		<cfif arguments.skip>
			<cfreturn arguments.jscode>
		</cfif>

		<cfset result = reReplace(result, "<!--[^>]+-->", "", "all")>
		<cfset result = reReplace(result, "[\s]{2,}", " ", "all")>
		<cfset result = rereplace(result, "[\s]*(\<|\>|\=)[\s]*", "\1", "all")>

		<cfreturn result>
	</cffunction>
<!---  --->
	<cffunction name="jsCompress" returntype="string">
		<cfargument name="jscode" type="string" required="yes">
		<cfargument name="skip" default="false" required="false" type="boolean">

		<cfif arguments.skip>
			<cfreturn arguments.jscode>
		</cfif>

		<cfset var result = trim(arguments.jscode)>

		<cfset result = rereplacenocase(result, "/\*", "#chr(172)#", "all")>
		<cfset result = rereplacenocase(result, "\*/", "#chr(172)#", "all")>
		<cfset result = rereplacenocase(result, "#chr(172)#[^#chr(172)#]*#chr(172)#", "", "all")>
		<cfset result = rereplace(result, "//[.]*[#chr(13)##chr(10)#]+", "", "all")>

		<cfset result = rereplace(result, "[\s]{2,}", " ", "all")>
		<cfset result = rereplacenocase(result, "[\s]*([\=|\{|\}|\(|\)|\;|[|\]|\+|\-]+)[\s]*", "\1", "all")>

		<cfreturn result>
	</cffunction>
<!---  --->


<cfif thisTag.executionMode EQ "START">
	<cfset request.javascript = arrayNew(1)>
	<cfset request.readyFunctions = arrayNew(1)>
	<cfset request.stylesheet = arrayNew(1)>

	<cfset cometURL = application.URL & "com/messenger.cfc?method=haveMessages">
	<cfsavecontent variable="cometFunction">
		<cfoutput>
			var messenger = setInterval(function() {
				$.get("#cometURL#", function(newMessages) {
					if (newMessages > 0) {
						$.get("#application.URL#/com/blazon/getMessages", function(messages) {
							var message;

							for (message in messages) {
								$("body").trigger(message["event"], messages["data"]);
							}
						});
					}
				})
			}, 1111);
		</cfoutput>
	</cfsavecontent>

	<cfset arrayAppend(request.javascript, cometFunction)>
<cfelseif thisTag.executionMode EQ "END">
	<cfset thisTag.generatedContent = HTMLcompress(thisTag.generatedContent)>

	<cfoutput>
		<cfif arraylen(request.stylesheet)>
			<cftry>
				<cfsavecontent variable="allCSS">
					<style type="text/css">
						#cssCompress(arrayToList(request.stylesheet, " "))#
					</style>
				</cfsavecontent>

				<cfhtmlhead text="#allCSS#">

			<cfcatch>
				<cfthrow detail="cf_blazon: unable to load CSS...">
			</cfcatch>

			</cftry>
		</cfif>

		<cfif arraylen(request.javascript) OR arraylen(request.readyFunctions)>
			<cftry>
				<cfsavecontent variable="allJS">
					<script language="JavaScript" type="text/javascript">
						<cfif arraylen(request.javascript)>
							#jsCompress(arrayToList(request.javascript, " "))#
						</cfif>
						<cfif arraylen(request.readyFunctions)>
							$(document).ready(function() {
								#jsCompress(arrayToList(request.readyFunctions, " "), 1)#
							});
						</cfif>
					</script>
				</cfsavecontent>

				<cfhtmlhead text="#allJS#">

			<cfcatch>
				<cfthrow detail="cf_blazon: unable to load javascript...">
			</cfcatch>

			</cftry>
		</cfif>
	</cfoutput>
<cfelse>
	<cfset thisTag.generatedContent = HTMLcompress(thisTag.generatedContent)>
</cfif>